package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.LiveProductDao;
import com.ytjj.qmyx.admin.dao.ProductActivityDao;
import com.ytjj.qmyx.admin.dao.ProductDao;
import com.ytjj.qmyx.admin.model.request.LiveProductRequest;
import com.ytjj.qmyx.admin.model.response.LiveProductResponse;
import com.ytjj.qmyx.admin.service.LiveProductService;
import com.ytjj.qmyx.mall.model.Product;
import com.ytjj.qmyx.mall.model.ProductActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class LiveProductServiceImpl implements LiveProductService {

    @Autowired
    private LiveProductDao liveProductDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductActivityDao productActivityDao;

    @Override
    public CommonPage<LiveProductResponse> getLiveProductList(LiveProductRequest liveProductRequest) {
        PageHelper.startPage(liveProductRequest.getPageNum(), liveProductRequest.getPageSize());
        List<LiveProductResponse> pageList = liveProductDao.selectList(liveProductRequest);
        if (!CollectionUtils.isEmpty(pageList)) {
            for (LiveProductResponse lpRes : pageList) {
                if (null != lpRes.getActivityFlag()) {
                    if (lpRes.getActivityFlag() == 0) {
                        Product product = productDao.getProductById(lpRes.getProductId());
                        lpRes.setProductNo(product.getProductNo());
                        lpRes.setProductName(product.getProductName());
                        lpRes.setProductImg(product.getProductImg());
                        lpRes.setDescr(product.getDescr());
                        lpRes.setStock(product.getStock());
                        lpRes.setWarehouse(product.getWarehouse());
                        lpRes.setWarehouseName(product.getWarehouseName());
                        lpRes.setWarehouseRule(product.getWarehouseRule());
                        lpRes.setAttributeJson(product.getAttributeJson());
                    } else if (lpRes.getActivityFlag() == 1) {
                        ProductActivity productActivity = productActivityDao.getProductActivityById(lpRes.getActivityId());
                        lpRes.setProductNo(productActivity.getProductNo());
                        lpRes.setProductName(productActivity.getProductName());
                        lpRes.setProductImg(productActivity.getProductImg());
                        lpRes.setDescr(productActivity.getDescr());
                        lpRes.setStock(productActivity.getStock());
                        lpRes.setWarehouse(productActivity.getWarehouse());
                        lpRes.setWarehouseName(productActivity.getWarehouseName());
                        lpRes.setWarehouseRule(productActivity.getWarehouseRule());
                        lpRes.setAttributeJson(productActivity.getAttributeJson());
                    } else {
                        throw new ApiException("数据异常，请检查数据是否正确!");
                    }
                }
            }
        }
        return CommonPage.restPage(pageList);
    }

    @Override
    public int insertLiveProduct(LiveProductRequest liveProductRequest) {
        //商品类型 activityFlag: 0-普通商品 1-活动商品
        if (liveProductRequest.getActivityFlag() == 0) {
            if(productDao.checkedProduct(liveProductRequest.getProductId()) <= 0){
                throw new ApiException("该商品不存在!");
            }
        } else {
            if(productActivityDao.checkedProductActivity(liveProductRequest.getActivityId()) <= 0){
                throw new ApiException("该活动商品不存在!");
            }else{
                System.out.println("活动商品Id=========" + liveProductRequest.getActivityId());
                ProductActivity productActivity = productActivityDao.getProductActivityById(liveProductRequest.getActivityId());
                System.out.println("商品Id======" + productActivity.getProductId());
                liveProductRequest.setProductId(productActivity.getProductId());
            }
        }
        System.out.println("商品Id获取到的值========" + liveProductRequest.getProductId());
        liveProductRequest.setCreateTime(new Date());
        return liveProductDao.insertLiveProduct(liveProductRequest);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        return liveProductDao.updateStatus(id, status);
    }

    @Override
    public int deleteLiveProduct(Integer id) {
        return liveProductDao.deleteLiveProduct(id);
    }

    @Override
    public int updateIsTop(Integer id, Integer liveId, Integer isTop) {
        //修改直播商品为置顶商品
        int count = liveProductDao.updateIsTop(id, isTop);
        if (count > 0 && isTop == 1) {
            //直播间置顶商品只能有一个，所以需将改直播间其他商品改为不置顶状态
            liveProductDao.batchIsTop(id, liveId);
        }
        return count;
    }

    @Override
    public int updateSort(Integer id, Byte sort) {
        return liveProductDao.updateSort(id, sort);
    }

}
